package com.lanyam.travel.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanyam.travel.base.dao.TravelMenuMapper;
import com.lanyam.travel.base.entity.TravelMenu;
import com.lanyam.travel.base.service.ITravelMenuService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rosson
 * @since 2020-02-24
 */
@Service
public class TravelMenuServiceImpl implements ITravelMenuService {

	@Autowired
	private TravelMenuMapper travelMenuMapper;
	
	@Override
	public List<TravelMenu> getAllFatherMenu() {
		// TODO Auto-generated method stub
		QueryWrapper<TravelMenu> wrapper = new QueryWrapper<TravelMenu>();
//		wrapper.isNull("father_menu_id");
		wrapper.eq("father_menu_id", "").or().isNull("father_menu_id");
		List<TravelMenu> selectList = travelMenuMapper.selectList(wrapper);
		return selectList;
	}

	@Override
	public int getFatherMenuMaxIndex() {
		// TODO Auto-generated method stub
		String index = travelMenuMapper.getFatherMenuMaxIndex();
		if(StringUtils.isEmpty(index))
		{
			return 0;
		}
		return Integer.parseInt(index);
	}

	@Override
	public int getChildMenuMaxIndex(String fatherId) {
		// TODO Auto-generated method stub
		String index = travelMenuMapper.getChildMenuMaxIndex(fatherId);
		if(StringUtils.isEmpty(index))
		{
			return 0;
		}
		return Integer.parseInt(index);
	}

	@Override
	public void addTravleMenu(TravelMenu menu) {
		// TODO Auto-generated method stub
		travelMenuMapper.insert(menu);
	}

	@Override
	public void updateMenu(TravelMenu menu) {
		// TODO Auto-generated method stub
		travelMenuMapper.updateById(menu);
	}

	@Override
	public TravelMenu getMenuById(String id) {
		// TODO Auto-generated method stub
		return travelMenuMapper.selectById(id);
		//return null;
	}

	@Override
	public void deleteByFatherId(String fatherId) {
		// TODO Auto-generated method stub
		QueryWrapper<TravelMenu> wrapper = new QueryWrapper<TravelMenu>();
		wrapper.eq("father_menu_id", fatherId);
		travelMenuMapper.delete(wrapper);
	}

	@Override
	public void deleteById(String id) {
		// TODO Auto-generated method stub
		travelMenuMapper.deleteById(id);
	}

	@Override
	public List<TravelMenu> getAllMenu() {
		// TODO Auto-generated method stub
		List<TravelMenu> allMenu = travelMenuMapper.selectAllMenu();
//		List<TravelMenu> allMenu = travelMenuMapper.selectList(null);
		Map<String,TravelMenu> menuMap =new HashMap<String, TravelMenu>();
		for (TravelMenu travelMenu : allMenu) {
			menuMap.put(travelMenu.getId(), travelMenu);
		}
		List<TravelMenu> childen = new ArrayList<TravelMenu>();
		List<TravelMenu> result = new ArrayList<TravelMenu>();
		TravelMenu father = null;
		for (TravelMenu travelMenu : allMenu) {
			String fatherId = travelMenu.getFatherMenuId();
			if(StringUtils.isEmpty(fatherId))
			{
				//如果自己是一级标签
				childen = travelMenu.getChilden();
				if(null == childen)
				{
					childen = new ArrayList<TravelMenu>();
					travelMenu.setChilden(childen);
				}
				result.add(travelMenu);
			}
			else 
			{
				//如果自己是二级标签,拿到父标签并把自己加入到父标签的子集中
				father = menuMap.get(fatherId);
				if(null != father)
				{
					childen = father.getChilden();
					if(null == childen)
					{
						childen = new ArrayList<TravelMenu>();
						father.setChilden(childen);
					}
					childen.add(travelMenu);
				}
			}
		}
		return result;
	}

	@Override
	public List<TravelMenu> searchMenu(String name) {
		// TODO Auto-generated method stub
//		QueryWrapper<TravelMenu> wrapper = new QueryWrapper<TravelMenu>();
//		wrapper.like("menu_name", name);
//		List<TravelMenu> selectList = travelMenuMapper.selectList(wrapper);
		List<TravelMenu> selectList = travelMenuMapper.searchMenu(name);
		return selectList;
	}

}
